#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "treasurer.h"

#include <QApplication>
#include <QTableView>
#include <QSqlTableModel>
#include <QSettings>

namespace
{
    static const QString S_GEOMETRY("Geometry");
    static const QString S_STATE("State");
    static const QString S_SPLITTER("Splitter");
}


MainWindow::MainWindow(QWidget *parent) 
:   QMainWindow(parent)
,   m_ui(new Ui::MainWindow)
,   m_treasurer(NULL)
{
    m_ui->setupUi(this);

    setWindowTitle(QApplication::applicationName() + " " + QApplication::applicationVersion());


    QSettings::setDefaultFormat(QSettings::IniFormat);

    QSettings s;
    restoreGeometry(s.value(S_GEOMETRY).toByteArray());
    restoreState(s.value(S_STATE).toByteArray());
    m_ui->splitter->restoreState(s.value(S_SPLITTER).toByteArray());

    m_treasurer = new Treasurer("test.db");
    if(!m_treasurer->isValid())
        exit(-1);

    m_ui->coinsTableView->setModel(m_treasurer->model());
}


MainWindow::~MainWindow()
{
    QSettings s;
    s.setValue(S_GEOMETRY, saveGeometry());
    s.setValue(S_STATE, saveState());
    s.setValue(S_SPLITTER, m_ui->splitter->saveState());

    delete m_treasurer;
}
